import { defineStore } from "pinia";
import axios from "axios";
import dayjs from "dayjs";

const useStore = defineStore({
  id: "hourStore",
  state: () => ({
    timeout: 5000,
    seatCodes: [
      "TM01",
      "TM02",
      "TM03",
      "TM04",
      "TM05",
      "TM06",
      "TM07",
      "TM08",
      "TM09",
      "TM10",
      "TM11",
      "TM12",
      "TM13",
      "TM14",
      "OMA01",
      // "OMA02",
      "SUP01",
      "SUP02",
      "PLANNER",
    ],
    users: [],
    generalViewConfigs: {
      type: "1100",
      custom: [dayjs().subtract(1, "day").toDate(), dayjs().toDate()],
      source: null,
      sourceBuffer: null,
      sourceSector: null,
    },
  }),
  getters: {},
  actions: {
    loadPersonalReport() {
      return null;
      if (this.users.length > 0) {
        axios
          .get("api/hours_personal_report.php?orgid=12")
          .then((res12) => {
            // console.log(res12.data);
            axios
              .get("api/hours_personal_report.php?orgid=13")
              .then((res13) => {
                // console.log(res13.data);
              })
              .catch((error) =>
                console.error("loading hour report error (room2)", error)
              );
          })
          .catch((error) =>
            console.error("loading hour report error (room1)", error)
          );
      } else {
        this.loadUsers();
      }
    },
    loadUsers() {
      return null;
      return new Promise((resolve, reject) => {
        axios
          .get("api/hours_get_users.php")
          .then((response) => {
            this.users = response.data;
            resolve(response.data);
          })
          .catch((error) => console.error("loading users error", error));
      });
    },
    get(
      start = dayjs().format("YYYY-MM-DD") + " 00:00:00",
      end = dayjs().add(1, "day").format("YYYY-MM-DD") + " 00:00:00",
      immediately = 0,
      full = 0
    ) {
      // let uri = `api/seat_hours_load.php?start=${start}&end=${end}&immediately=${immediately}`;
      return new Promise((resolve, reject) => {
        axios
          .request({
            url: "api/seat_hours_load.php",
            params: { start, end, immediately, full },
            timeout: this.timeout,
          })
          .then((response) => {
            console.log(
              "workingHoursGet",
              `from ${start} to ${end}`,
              response.data
            );
            resolve(response.data);
          })
          .catch((error) => {
            console.error("workingHoursGet", `from ${start} to ${end}`, error);
            reject(error);
          });
      });
    },
    load(dates = null, forced = false, codes = null) {
      return null;
      this.loadPersonalReport();
      return new Promise((resolve, reject) => {
        if (dates == null) {
          dates = [
            dayjs().format("YYYY-MM-DD"),
            dayjs().subtract(1, "day").format("YYYY-MM-DD"),
          ];
        }
        axios
          .request({
            url: "api/seat_hours_load.php",
            method: "post",
            data: { dates, codes, forced },
            timeout: this.timeout,
          })
          .then((response) => {
            console.log("workingHoursLoading", response.data);
            resolve(response.data);
          })
          .catch((error) => {
            console.error("workingHoursLoading", error);
            reject(error);
          });
      });
    },
  },
  loadSeatHours() {
    // TODO: implement
  },
  getSeatHours(
    start = dayjs().subtract(24, "hour").format("YYYY-MM-DD HH:mm:ss"),
    end = dayjs().format("YYYY-MM-DD HH:mm:ss")
  ) {
    return new Promise((resolve, reject) => {
      axios
        .request({
          method: "get",
          url: "api/seat_hours_get.php",
          params: { start, end },
        })
        .then((response) => {
          console.log(
            "getSeatHours",
            `from ${start} to ${end}`,
            response.data
            /**
             *  $output = [
             *    'start' => $start,
             *    'end' => $end,
             *    'count' => count($result),
             *    'content' => $result,
             *    'users' => $users,
             *    'usersGrouped' => $users_grouped,
             *    // 'sql' => $sql,
             *  ];
             */
          );
          resolve(response.data);
        })
        .catch((error) => {
          console.error("getSeatHours", `from ${start} to ${end}`, error);
          reject(error);
        });
    });
  },
});

export default useStore;
